<template>
	<view class="">
		<view class="add_pingjia">
			<view class="info_top">
				<view class="item_title">{{ infoObj.jingquName }}</view>
				<view class="item_center">
					<image :src="infoObj.jingquPictureUrl">
					</image>
					<view class="i_c_right">
						<view v-for="(i, index) in infoObj.orderDetailsVos" :key="index">
							<view class="name_price">
								<text class="name2">{{ i.menpiaoCategoryName }}</text>
								<text class="price">￥{{ i.orderPrice }}</text>
							</view>
							<view class="name_price time_num">
								<text>
									<text>日期：</text>
									{{ i.date }}
								</text>
								<text>×{{ i.orderNum }}</text>
							</view>
							<!-- <view class="time2">
								<text v-if="infoObj.isJiuDian">入离时间：</text>
								<text v-else>时间：</text>
								<text>{{ infoObj.time2 }}</text>
							</view>
							<view class="label">
								<text v-if="infoObj.isJiuDian && infoObj.label">
									<text class="label_item" v-for="(i2, index2) in infoObj.label.split(',')" :key="index2">{{ i2 }}</text>
								</text>
							</view> -->
						</view>
					</view>
				</view>
			</view>
			<view class="fenshu">
				<text>评分</text>
				<u-rate v-model="bsPingjia.pingjiaScore" size='20' activeColor="#f6b92a" allowHalf></u-rate>
			</view>
			<uni-easyinput type="textarea" autoHeight v-model="bsPingjia.pingjiaContent" maxlength="100" cursorSpacing="10"
				placeholder="分享一下你本次的游玩体验吧" :styles='styles' inputBorder></uni-easyinput>
			<view class="upload_img">
				<u-upload :fileList="imgUrl" @delete="deletePic" multiple :maxCount="9" :previewFullImage="true"
					@afterRead="afterRead">
					<image :src="config.imgUrl+'upload.jpg'" mode="widthFix" style="width: 686rpx;height: 216rpx;">
					</image>
				</u-upload>
			</view>
		</view>
		<view class="upload_add">
			<u-button text="发布评价" shape="circle" color='#0E9589' :loading="loading" loadingText="提交中" @click="uploadAdd">
			</u-button>
		</view>
	</view>
</template>

<script>
import config from '@/config'
import { getToken } from "@/utils/auth";
import { addPingjiaApi } from "@/api/mine/waitingComment"

export default {
	data() {
		return {
			config,
			loading: false,
			styles: {
				color: "#000"
			},
			infoObj: {},
			bsPingjia: {
				avatar: "", // 评价人头像
				jingquId: 0,  //景区ID
				// menpiaoCategory: "",//	门票类型
				orderId: 0,//	订单ID

				pingjiaScore: 5, // 分数
				pingjiaContent: "", //内容
				pingjiaPictureUrl: "", // 评价图片，多个图片,号拼接
			},
			imgUrl: []
		};
	},

	onLaunch() { },

	onLoad(params) {
		const data = JSON.parse(params.data)
		console.log(data, "datadata")
		this.infoObj = data;
	},
	methods: {
		// 读取后的处理函数
		async afterRead(event) {
			// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
			let lists = [].concat(event.file)
			let fileListLen = this[`imgUrl${event.name}`].length
			lists.map((item) => {
				this[`imgUrl${event.name}`].push({
					...item,
					status: 'uploading',
					message: '上传中'
				})
			})
			for (let i = 0; i < lists.length; i++) {
				const result = await this.uploadFilePromise(lists[i].url)

				let item = this[`imgUrl${event.name}`][fileListLen]
				this[`imgUrl${event.name}`].splice(fileListLen, 1, Object.assign(item, {
					status: 'success',
					message: '',
					url: result.url
				}))
				fileListLen++
			}
		},
		uploadFilePromise(url) {
			return new Promise((resolve, reject) => {
				let a = uni.uploadFile({
					url: `${config.baseUrl}/common/upload`,
					filePath: url,
					name: 'file',
					header: {
						Authorization: 'Bearer ' + getToken()
					},
					success: (res) => {
						console.log(JSON.parse(res.data))
						setTimeout(() => {
							resolve(JSON.parse(res.data).data)
						}, 1000)
					}
				});
			})
		},

		// 删除上传的图片
		deletePic(e) {
			console.log(e)
			this.imgUrl.splice(e.index, 1)
		},

		// 上传按钮
		uploadAdd() {
			console.log(this.imgUrl)
			if (this.imgUrl.length) {
				const pingjiaPictureUrl = this.imgUrl.map((item) => {
					return item.url;
				});
				console.log(pingjiaPictureUrl)
				this.bsPingjia.pingjiaPictureUrl = pingjiaPictureUrl.join(",")
			} else {
				this.bsPingjia.pingjiaPictureUrl = '';
			}
			this.bsPingjia.avatar = JSON.parse(uni.getStorageSync('userInfo')).avatarUrl;
			this.bsPingjia.jingquId = this.infoObj.jingquId;
			this.bsPingjia.orderId = this.infoObj.orderId;

			this.loading = true;
			addPingjiaApi(this.bsPingjia)
				.then((res) => {
					console.log(res)
					if (res.code == 200) {
						uni.showToast({
							title: "新增成功"
						});
						uni.navigateBack({
							delta: 1
						});
					} else {
						uni.showToast({
							title: "新增失败",
							icon: "error"
						});
					}
					this.loading = false;
				})
				.catch((err) => {
					console.log(err)
					uni.showToast({
						title: "新增失败",
						icon: "error"
					});
					this.loading = false;
				})
		}
	},
};
</script>

<style lang="scss">
page {
	background-color: #fff;
}

.add_pingjia {
	width: 750rpx;
	padding: 16rpx 30rpx 0 30rpx;
	position: relative;

	.info_top {
		margin: 0 auto;


		.item_title {
			font-size: 36rpx;
			color: #333333;
			line-height: 50rpx;
			margin-bottom: 24rpx;
			font-weight: bolder;
		}

		.item_center {
			width: 100%;
			display: flex;
			align-items: flex-start;

			image {
				display: inline-block;
				width: 176rpx !important;
				height: 176rpx;
				border-radius: 12rpx;
				margin-right: 22rpx;
			}

			.i_c_right {
				flex: 2.2;

				.name_price {
					width: 100%;
					display: flex;
					align-items: center;
					justify-content: space-between;

					.name2,
					.price {
						font-size: 32rpx;
						color: #333333;
						line-height: 45rpx;
						font-weight: bolder;
					}

					.price {
						font-size: 24rpx;
					}
				}

				.time_num,
				.time2 {
					margin: 10rpx 0;
					font-size: 24rpx;
					color: #999999;
				}

				.label {
					.label_item {
						padding: 6rpx 16rpx;
						margin-right: 16rpx;
						color: #FF841C;
						font-size: 20rpx;
						height: 32rpx;
						background: rgba(255, 242, 230, 1);
					}
				}
			}
		}

		.item_bottom {
			margin-top: 36rpx;
			text-align: right;

			.price2 {
				font-size: 28rpx;
				font-weight: bolder;
				color: #333333;

				.price2_num {
					font-size: 32rpx;
					color: #FF841C;
				}
			}

			._pingjia {
				text-align: right;
				margin-top: 32rpx;
				height: 64rpx;
				line-height: 64rpx;

				text {
					display: inline-block;
					width: 176rpx;
					text-align: center;
					background: #FFFFFF;
					border-radius: 164rpx;
					border: 2rpx solid #999999;
					font-size: 28rpx;
					color: #999999;
				}
			}
		}
	}

	.fenshu {
		display: flex;
		align-items: center;
		margin: 42rpx 0;

		text {
			font-size: 32rpx;
			color: #333333;
		}
	}

	.upload_img {
		margin-top: 20rpx;
	}

}

.upload_add {
	height: 144rpx;
	margin-top: 120rpx;
	border-top: 1rpx solid #E7E4E4;
	position: relative;
}

// 样式重置
.u-upload__wrap {
	// justify-content: space-between;
}

.u-upload__wrap__preview {
	margin: 0 0 18rpx 18rpx !important;

	&:nth-child(3n-2) {
		margin-left: 0 !important;
	}
}

.u-upload__wrap__preview__image {
	width: 218rpx !important;
	height: 218rpx !important;
}

.u-upload__button {
	width: 100% !important;
}

.u-button {
	width: 288rpx !important;
	height: 64rpx !important;
	position: absolute;
	top: 50%;
	transform: translateY(-50%);
}
</style>
